package activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.jaden.washer.*;
import com.example.jaden.washer.ActivityManagement;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;

import student.Student;


public class InformationModificationPage extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    EditText consignee;
    EditText phonenumber;
    EditText address;
    RadioGroup sexSelect;
    RadioButton sexmale;
    RadioButton sexfemale;
    Button save;
    MyApplication application;
    Student student;
    String sex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personnalinfo_modify_page);
        //将当前活动加入list中进行管理
        ActivityManagement.activityList.add(this);

        //获取Toolbar的实例
        Toolbar toolbar=(Toolbar)findViewById(R.id.personalinfomodifypage_toolbar);
        setSupportActionBar(toolbar);

        //获取控件实例
        getInstance();
        //执行按键点击监听
        setClickListener();
        initPersonInformation();
    }
    //按back键 销毁活动
    protected void onDestroy() {
        ActivityManagement.KillOneActivity();
        super.onDestroy();
    }

    //获取控件实例
    private void getInstance() {
        consignee=(EditText)findViewById(R.id.editText_consignee_personalinfomodifypage);
        phonenumber=(EditText)findViewById(R.id.editText_phonenumber_personalinfomodifypage);
        address=(EditText)findViewById(R.id.editText_address_personalinfomodifypage);
        save=(Button)findViewById(R.id.button_save_personalinfomodifypage);
        sexSelect=(RadioGroup)findViewById(R.id.radioGroup_sex_personalinfomodifypage);
        sexmale=(RadioButton)findViewById(R.id.radioButton_sexmale_personalinfomodifypage);
        sexfemale=(RadioButton)findViewById(R.id.radioButton_sexfemale_personalinfomodifypage);
        application=MyApplication.getInstance();
    }
    //执行按键点击监听
    private void setClickListener(){
        save.setOnClickListener(this);
        sexmale.setOnCheckedChangeListener(this);
        sexfemale.setOnCheckedChangeListener(this);
    }
    private void initPersonInformation()
    {


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_save_personalinfomodifypage:
                String consig,phonum,addr;
                consig=consignee.getText().toString();
                phonum=phonenumber.getText().toString();
                addr=address.getText().toString();

                String url="http://47.96.159.236:8080/Test/Citizen?method=updateCitizenInfor";
                try {
                    //打包Json数据
                    JSONObject userJson=new JSONObject();
                    userJson.put("citizenID",application.getStudent().getStuID());
                    userJson.put("citizenUsername",consig);
                    userJson.put("citizenPhone",phonum);
                    userJson.put("citizenGender",sex);
                    userJson.put("citizenAddress",addr);
                    String messageJson=String.valueOf(userJson);
                    TransferData.SavePersonalInformation(url,messageJson);
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }

                while(true) {
                    if (application.isSavePersonalInfoOK()) {
                        application.setSavePersonalInfoOK(false);
                        verification(application.getResponseMessage());

                        break;
                    }
                }

                break;
        }
    }
    public void verification(ResponseMessage responseMessage)
    {
        switch (responseMessage.getCode()) {
            case 1050:
                Toast.makeText(this, responseMessage.getMsg(), Toast.LENGTH_LONG).show();
                Intent intent=new Intent(this, PersonalinfoPage.class);
                startActivity(intent);
                break;
            case 110:
                Toast.makeText(this, responseMessage.getMsg(), Toast.LENGTH_LONG).show();
                break;
            case 1051:
                Toast.makeText(this, responseMessage.getMsg(), Toast.LENGTH_LONG).show();
                break;
            case 1052:
                Toast.makeText(this, responseMessage.getMsg(), Toast.LENGTH_LONG).show();
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId())
        {
            case R.id.radioButton_sexmale_personalinfomodifypage:
                if(isChecked)
                {
                    sex="男";
                }
                break;
            case R.id.radioButton_sexfemale_personalinfomodifypage:
                if(isChecked)
                {
                    sex="女";
                }
                break;
        }
    }
}
